/*
** Base file handle
**
** TODO: Bugfix this and convert drivers to use it
*/

#include "smse2kincludes.h"
#include "FileSystem.h"
#include "File.h"

int CBaseFile::Open(const char *filename) {

	// If we're using CBaseFile we can only read
	fHandle = filesystem.OpenFile(filename,FSYS_PERMISSION_READ);
	return fHandle ? 0 : 1;

}

CMemory* CBaseFile::GetEntireFile() {

	// Max filesize here is 50 MB just to be on the safe side of things
	UINT64 fsize = filesystem.GetFileSize(fHandle);
	if (fsize > 52428800) {
		Msg("CBaseFile: Cannot allocate memory for file (Size exceeds 50 MB)\n");
		return NULL;
	}

	CMemory* mem = new CMemory;
	if (mem->AllocMemory(fsize)) {
		Msg("CBaseFile: Allocation of memory failed! (Wanted %d bytes)\n");
		delete[] mem;
		return NULL;
	}

	filesystem.Read(fHandle, mem->GetRawMemory(), fsize);


	filesystem.CloseFile(fHandle);
	return mem;
}